package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.dao.IShoppingCartDao;
import com.zhijiaoyun.model.ShoppingCart;
import com.zhijiaoyun.model.dto.CourseDto;
import com.zhijiaoyun.model.dto.NewsDto;
import com.zhijiaoyun.model.dto.ShoppingCartDto;
import com.zhijiaoyun.service.ICourseService;
import com.zhijiaoyun.service.INewsService;
import com.zhijiaoyun.service.IShoppingCartService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "shoppingCartService")
public class ShoppingCartServiceImpl implements IShoppingCartService {

	@Resource
	IShoppingCartDao shoppingCartDao;
	
	@Resource
	ICourseService courseService;
	
	@Resource
	INewsService newsService;
	
	@Override
	public ShoppingCart getShoppingCartByIds(Long goodsId, Long userId, int goodsType) {
		return shoppingCartDao.selectByShoppingCartByIds(goodsId, userId, goodsType);
	}

	@Override
	public void addShoppingCart(ShoppingCart cart) {
		shoppingCartDao.insert(cart);
	}

	@Override
	public void updateShoppingCart(ShoppingCart cart) {
		shoppingCartDao.updateByPrimaryKey(cart);
	}

	@Override
	public ShoppingCart getShoppingCartByCartIdUserId(Long cartId, Long userId) {
		return shoppingCartDao.selectByCartIdUserId(cartId, userId);
	}

	@Override
	public void deleteShoppingCart(ShoppingCart cart) {
		shoppingCartDao.deleteByPrimaryKey(cart.getCartId());
	}

	@Override
	public List<ShoppingCart> getByUserId(Integer pageIndex, Long userId) {
		return null;
	}

	@Override
	public List<ShoppingCartDto> getDtoByUserId(Integer pageIndex, Long userId) {
		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}
		int beginIndex = (pageIndex - 1) * DsConstant.PAGESIZE_DEFAULT;
		List<ShoppingCartDto> shoppingCartDtos = new ArrayList<>();
		List<ShoppingCart> shoppingCarts = shoppingCartDao.selectByUserId(beginIndex, DsConstant.PAGESIZE_DEFAULT, userId);
		if (!StringUtils.isEmptyList(shoppingCarts)) {
			for (ShoppingCart shoppingCart : shoppingCarts) {
				// 收藏的对象
				if (shoppingCart.getGoodsType() == 1) {
					CourseDto courseDto = courseService.getCourseDtoByCourseId(shoppingCart.getGoodsId());
					if (courseDto != null) {
						ShoppingCartDto shoppingCartDto = new ShoppingCartDto(shoppingCart);
						shoppingCartDto.setCourseDto(courseDto);
						shoppingCartDtos.add(shoppingCartDto);
					}
				} else {
					NewsDto newsDto = newsService.getNewsDtoByNewsId(shoppingCart.getGoodsId());
					if (newsDto != null) {
						ShoppingCartDto shoppingCartDto = new ShoppingCartDto(shoppingCart);
						shoppingCartDto.setNewsDto(newsDto);
						shoppingCartDtos.add(shoppingCartDto);
					}
				}
			}
		}
		return shoppingCartDtos;
	}

	@Override
	public List<ShoppingCart> getShoppingCartByCartIds(List<String> cartIds) {
		return shoppingCartDao.selectByCartIds(cartIds);
	}

}
